﻿using System;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using JTLeigh.Commerce.Spencer.Features.ProductImages.Common.Entities;
using JTLeigh.Commerce.Spencer.Features.ProductImages.Business.Caches;

namespace JTLeigh.Commerce.Spencer.Features.ProductImages.Business.UnitTests.Caches
{

	/// <summary>
	/// Unit tests for the <see cref="InMemoryProductImageCache"/> class.
	/// </summary>
	[TestClass]
	public class InMemoryProductImageCacheFixture
	{

		#region Fields

		private static Guid TestTenant1Id = Guid.NewGuid();
		private static Guid TestTenant2Id = Guid.NewGuid();
        private const string TestCatalogId = "BaseCatalog";
        private const string TestProduct1Id = "123";
		private const string TestProduct2Id = "456";

		#endregion // Fields

		#region Methods

		#region ProductImages

		/// <summary>
		/// Ensures <see cref="InMemoryProductImageCache.GetProductImages"/> and
		/// <see cref="InMemoryProductImageCache.SetProductImages"/> get and set images in the 
		/// cache as expected for multiple products for a single tenant.
		/// </summary>
		/// <remarks>
		/// <para>
		/// This test uses two different product identifiers in the same tenant.
		/// </para>
		/// </remarks>
		[TestMethod]
		public void TestGetSetProductImagesSingleTenant()
		{
			InMemoryProductImageCache cache;
			Dictionary<Guid, ProductImage> images1;
			Dictionary<Guid, ProductImage> images2;

			cache = new InMemoryProductImageCache();
			Assert.IsNull(cache.GetProductImages(TestTenant1Id, TestCatalogId, TestProduct1Id));
			Assert.IsNull(cache.GetProductImages(TestTenant1Id, TestCatalogId, TestProduct2Id));

			images1 = new Dictionary<Guid, ProductImage>();
			images1.Add(Guid.NewGuid(), new ProductImage());
			images1.Add(Guid.NewGuid(), new ProductImage());
			images1.Add(Guid.NewGuid(), new ProductImage());
			cache.SetProductImages(TestTenant1Id, TestCatalogId, TestProduct1Id, images1);
			images2 = new Dictionary<Guid, ProductImage>();
			images2.Add(Guid.NewGuid(), new ProductImage());
			images2.Add(Guid.NewGuid(), new ProductImage());
			images2.Add(Guid.NewGuid(), new ProductImage());
			cache.SetProductImages(TestTenant1Id, TestCatalogId, TestProduct2Id, images2);
			Assert.AreSame(images1, cache.GetProductImages(TestTenant1Id, TestCatalogId, TestProduct1Id));
			Assert.AreSame(images2, cache.GetProductImages(TestTenant1Id, TestCatalogId, TestProduct2Id));
		}

		/// <summary>
		/// Ensures <see cref="InMemoryProductImageCache.GetProductImages"/> and
		/// <see cref="InMemoryProductImageCache.SetProductImages"/> get and set images in the 
		/// cache as expected for a single product for multiple tenants.
		/// </summary>
		/// <remarks>
		/// <para>
		/// This test uses the same product identifier in two different tenants, ensuring 
		/// isolation between tenant cache partitions.
		/// </para>
		/// </remarks>
		[TestMethod]
		public void TestGetSetProductImagesMultipleTenants()
		{
			InMemoryProductImageCache cache;
			Dictionary<Guid, ProductImage> images1;
			Dictionary<Guid, ProductImage> images2;

			cache = new InMemoryProductImageCache();
			Assert.IsNull(cache.GetProductImages(TestTenant1Id, TestCatalogId, TestProduct1Id));
			Assert.IsNull(cache.GetProductImages(TestTenant2Id, TestCatalogId, TestProduct1Id));

			images1 = new Dictionary<Guid, ProductImage>();
			images1.Add(Guid.NewGuid(), new ProductImage());
			images1.Add(Guid.NewGuid(), new ProductImage());
			images1.Add(Guid.NewGuid(), new ProductImage());
			cache.SetProductImages(TestTenant1Id, TestCatalogId, TestProduct1Id, images1);
			images2 = new Dictionary<Guid, ProductImage>();
			images2.Add(Guid.NewGuid(), new ProductImage());
			images2.Add(Guid.NewGuid(), new ProductImage());
			images2.Add(Guid.NewGuid(), new ProductImage());
			cache.SetProductImages(TestTenant2Id, TestCatalogId, TestProduct1Id, images2);
			Assert.AreSame(images1, cache.GetProductImages(TestTenant1Id, TestCatalogId, TestProduct1Id));
			Assert.AreSame(images2, cache.GetProductImages(TestTenant2Id, TestCatalogId, TestProduct1Id));
		}

		#endregion // ProductImages

		#endregion // Methods

	}

}
